#include "stdafx.h"
#include "SORoughSet.h"
#include "SOPMI.h"
void CSORoughSet::init(const CString& workDir,double alpha,bool fromOriginal)
{
	m_alpha=alpha;
	m_workDir=workDir;
	m_bFromOriginal=fromOriginal;
	initWIL();
	initNeg();	
}

void CSORoughSet::traverse(const CString &dir,int textNum)
{
	m_rs.samples.clear();
	CFileFind oFindDir;
	BOOL b_dir=oFindDir.FindFile(dir+CString("\\*.*"));
	int classId=-1;
	int totalNum[2]={textNum/2,textNum/2};
	
	while(b_dir)
	{
		b_dir=oFindDir.FindNextFile();
		if(oFindDir.IsDirectory()&&!oFindDir.IsDots())
		{
			classId++;
			int num=0;
			CFileFind oFindTxt;
			BOOL  b_txt=oFindTxt.FindFile(oFindDir.GetFilePath()+CString("\\*.*"));
			while(b_txt)
			{
				b_txt=oFindTxt.FindNextFile();
				if(!oFindTxt.IsDirectory()&&!oFindTxt.IsDots())
				{
					CStdioFile iFile;
					CString filename=oFindTxt.GetFilePath();
					if(!iFile.Open(filename,CStdioFile::modeRead))
						continue;
					std::vector<CString> wordList;
					std::vector<int> posList;
					while(iFile.GetPosition()!=iFile.GetLength())
					{
						CString tmp;
						iFile.ReadString(tmp);
						CString name;
						int pos;
						sscanf(tmp,"%s %d",name,&pos);
						CString x;
						x.Format("%s",name);
						wordList.push_back(x);
						posList.push_back(pos);
					}
					iFile.Close();
					//////////////////////
					double rate=0.0;
					Sample sm;
					MU terms;
					for(int i=0;i<(int)wordList.size();i++)
					{
						wordSingleCountMap::iterator iter=m_wordIdList.find(wordList[i]);
						if(iter==m_wordIdList.end()) 
							continue;			
						int fac=1;
						for(int j=i-1;j>=0;j--)
						{
							if(posList[i]-posList[j]>NEGDIST)
								break;
							else if(isNeg(wordList[j]))
							{
								fac=-1;
								break;
							}
						}
						MUI titer=terms.find(iter->second);
						if(titer!=terms.end())
							titer->second+=fac;
						else
							terms.insert(MUP(iter->second,fac));
						if(!m_bFromOriginal)
							rate+=fac*m_words[iter->second].rate;
					}
					if(!terms.empty())
					{
						sm.terms=terms;
						num++;
						if(m_bFromOriginal)
							sm.label=classId;
						else
							sm.label=rate>=0?1:0;
						m_rs.samples.push_back(sm);
						if(num==totalNum[classId])
						break;
					}
					
				}
			}
			oFindTxt.Close();
		}
	}
	oFindDir.Close();
	m_rs.txtNum=textNum;
}
void CSORoughSet::reduceAttr()
{
	LI cls;
	int num;
	LI results=m_rs.ReduceAttr(num,cls);
	CStdioFile oFile1,oFile2;
	oFile1.Open(m_workDir+"//guide.dat",CStdioFile::modeWrite|CStdioFile::modeCreate);
	oFile2.Open(m_workDir+"//misguide.dat",CStdioFile::modeWrite|CStdioFile::modeCreate);
	double thresh=m_alpha;
	for(int i=0;i<num;i++)
	{
		int id=results.front();
		CString str;
		if((m_words[id].classId==0&&m_words[id].rate<=thresh)||
		   (m_words[id].classId==1&&m_words[id].rate>=thresh))
		{
			str.Format("%s\n",m_words[id].name);
			oFile1.WriteString(str);
		}
		else
		{
			str.Format("%s\n",m_words[id].name);
			oFile2.WriteString(str);
		}
		results.pop_front();
	}
	oFile1.Close();
	oFile2.Close();
	return;
}
void CSORoughSet::initWIL()
{
	CStdioFile iFile;
	std::vector<CString> swtFilename;
	swtFilename.push_back(m_workDir+"\\neg.txt");
	swtFilename.push_back(m_workDir+"\\pos.txt");
	int seq=0;
	for(int i=0;i<(int)swtFilename.size();i++)
	{
		if(!iFile.Open(swtFilename[i],CStdioFile::modeRead))
			return ;
		while(iFile.GetPosition()!=iFile.GetLength())
		{
			CString tmp;
			iFile.ReadString(tmp);
			char buffer[256];
			float rate;
			sscanf(tmp,"%s %f",buffer,&rate);
			m_wordIdList.insert(wordSingleCountPair(CString(buffer),seq++));
			wordInfo wr;
			wr.name=buffer;
			wr.rate=rate;
			wr.classId=i;
			m_words.push_back(wr);
		}
		iFile.Close();
	}
	m_rs.clsNum=2;
	m_rs.dimen=(int)m_wordIdList.size();
}
void CSORoughSet::initNeg()
{
	CStdioFile iFile;
	iFile.Open(".\\storedData\\negWords.txt",CStdioFile::modeRead);
	while(iFile.GetPosition()!=iFile.GetLength())
	{
		CString tmp;
		iFile.ReadString(tmp);
		CString x;
		x.Format("%s",tmp);
		m_negs.push_back(x);
	}
	iFile.Close();
}
bool CSORoughSet::isNeg(const CString &word)
{
	for(int i=0;i<(int)m_negs.size();i++)
	{
		if(m_negs[i]==word)
			return true;
	}
	return false;
}